<template>
  <div class="changeAdd">
    <van-nav-bar
      title="收货地址"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <main>
      <van-address-edit
        :area-list="areaList"
        show-delete
        show-set-default
        show-search-result
        :search-result="searchResult"
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
        @delete="onDelete"
        @change-detail="onChangeDetail"
        :address-info=addlist
      />
    </main>
  </div>
</template>

<script>
import { Toast } from "vant";
import { areaList } from "@vant/area-data";
import {address_del,address_edit} from "../../api/index2"

export default {
  data() {
    return {
      areaList,
      searchResult: [],
      addlist:{
        name:'',
        tel:'',
        addressDetail:'',
        addressid:'',
        areaCode:''
      }
    };
  },
  created(){
    this.addlist.name = this.$route.query.name;
    this.addlist.areaCode = this.$route.query.areaCode;
    this.addlist.tel = this.$route.query.tel;
    this.addlist.addressDetail = this.$route.query.addressDetail;
    this.addlist.addressid = this.$route.query.addressid;
  },
  methods: {
    onClickLeft() {
      this.$router.back();
    },
    onSave() {
      Toast('save');
      //编辑收获地址
      // userid 用户id ; addressid 地址id ; address 联系地址 ; phone 联系方式 ; name 联系人 都是必传参数
      address_edit({
        userid:localStorage.getItem("userid"),
        addressid:this.addlist.addressid,
        address:localStorage.getItem("areaCode"),
        phone:this.addlist.tel,
        name:this.addlist.name
      }).then((res)=>{
        if(res.data.code == 200){
          this.$router.go(-1);
        }
      })
    },
    onDelete() {
      //删除收获地址
      address_del({userid:localStorage.getItem("userid"),addressid:this.addlist.addressid}).then((res)=>{
        if(res.data.code == 200){
          this.$router.go(-1);
        }
      })
    },
    onChangeDetail(val) {
      if (val) {
        this.searchResult = [
          {
            name: '黄龙万科中心',
            address: '杭州市西湖区',
          },
        ];
      } else {
        this.searchResult = [];
      }
    },
  },
};
</script>

<style scoped>
.changeAdd {
  height: 100%;
}
</style>